<?php

namespace api\modules\Wechat\modules\Payment\controllers;

use api\models\c2\entity\VoteRecordModel;
use common\helpers\DeviceLogHelper;
use common\models\c2\entity\ActivityPlayerModel;
use common\models\c2\entity\ActivityPlayerVoteRecordModel;
use common\models\c2\entity\FeUserModel;
use common\models\c2\entity\GiftModel;
use common\models\c2\entity\GiftOrderModel;
use common\models\c2\statics\ActivityPlayerState;
use common\models\c2\statics\OrderPaymentType;
use common\models\c2\statics\OrderPayState;
use common\models\c2\statics\VoteType;
use common\models\c2\statics\Whether;
use common\rest\statics\ResponseDatum;
use EasyWeChat\Payment\Order;
use Mpdf\Tag\P;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\Response;

/**
 * Default controller for the `payment` module
 */
class DefaultController extends \common\rest\controllers\AuthActiveController
{
    public $enableCsrfValidation = false;

    public $modelClass = 'common\models\c2\entity\GiftOrderModel';

    public function behaviors() {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' => \common\rest\filters\auth\CompositeAuth::className(),
                'authMethods' => [
                    \common\rest\filters\auth\HttpBearerAuth::className(),
                    \common\rest\filters\auth\QueryParamAuth::className(),
                ],
            ],
        ]);
    }

    /**
     * Renders the index view for the module
     * @return array
     * @throws \yii\db\Exception
     */
    public function actionIndex()
    {
        if (!Yii::$app->request->isPost) {
            return ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Method disable.')], false);
        }
        $params = Yii::$app->request->post();
        $playerModel = ActivityPlayerModel::findOne($params['player_id']);
        if ($playerModel->state == ActivityPlayerState::STATE_NOT_CHECK) {
            return ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Player disable.')], false);
        }
        $activityModel = $playerModel->activity;
        if ($activityModel->is_released != Whether::TYPE_YES || time() > strtotime($activityModel->end_at)) {
            return ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Activity disable.')], false);
        }
        $user = Yii::$app->user->identity;
        $giftModel = GiftModel::findOne($params['gift_id']);
        if ($giftModel) {
            $transaction = Yii::$app->db->beginTransaction();
            $orderModel = new GiftOrderModel();
            $orderModel->loadDefaultValues();
            $attributes = [
                'customer_id' => $user->id,
                'pay_method' => OrderPaymentType::TYPE_WECHAT_PAY,
                'activity_id' => $activityModel->id,
                'activity_player_id' => $playerModel->id,
                'discount_rate' => 0,
                'discount_money' => 0,
                'gift_id' => $giftModel->id,
                'gift_name' => $giftModel->name,
                'obtain_score' => $giftModel->obtain_score,
                'obtain_vote_number' => $giftModel->obtain_vote_number,
                'remote_ip' => Yii::$app->request->userIP,
                'memo' => '',
                'pay_price' => $giftModel->price,
                'src_type' => DeviceLogHelper::getDeviceType(),
                'state' => OrderPayState::UN_PAY
            ];
            $orderModel->setAttributes($attributes);
            if ($orderModel->save()) {
                $payment = Yii::$app->wechat->payment;
                $conf = [
                    'trade_type' => 'JSAPI', // JSAPI，NATIVE，APP...
                    'body' => Yii::t('app.c2', 'Pay Body'),
                    'detail' => Yii::t('app.c2', 'Gift to {s1} pay for {s2}', [
                        's1' => $activityModel->title,
                        's2' => $giftModel->name,
                    ]),
                    'out_trade_no' => $orderModel->code,
                    'total_fee' => $orderModel->pay_price * 100, // 单位：分 * 100 = 元
                    'notify_url' => API_BASE_URL . '/wechat/payment/default/payment-notify',// 支付结果通知网址，如果不设置则会使用配置里的默认地址
                    'openid' => $user->wechat_open_id, // trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识，
                ];
                $wxPayOrder = new Order($conf);
                $prepayRequest = $payment->prepare($wxPayOrder);
                if ($prepayRequest->return_code = 'SUCCESS' && $prepayRequest->result_code == 'SUCCESS') {
                    $transaction->commit();
                    $prepayId = $prepayRequest->prepay_id;
                    $jsApiConfig = $payment->configForPayment($prepayId);
                    return $jsApiConfig;
                } else {
                    $transaction->rollBack();
                    // throw new yii\base\ErrorException('微信支付异常, 请稍后再试');
                    return ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Wechat Pay Error')], $prepayRequest);
                }
            } else {
                $transaction->rollBack();
                return ResponseDatum::getErrorDatum([], $orderModel->errors);
            }
        }
        return ResponseDatum::getErrorDatum(['message' => Yii::t('app.c2', 'Vote Fail')], false);
    }

    public function actionPaymentNotify()
    {
        $response = Yii::$app->wechat->payment->handleNotify(function ($notify, $success) {
            $order = GiftOrderModel::findOne(['code' => $notify->out_trade_no]);
            if ($order->state == OrderPayState::PAY) {
                return 'ok';
            }
            $attributes = [
                'type' => VoteType::TYPE_GIFT,
                'user_id' => $order->customer_id,
                'activity_player_id' => $order->activity_id,
                'vote_number' => $order->obtain_vote_number,
                'order_id' => $order->id,
                'gift_id' => $order->gift_id,
                'gift_name' => $order->gift_name,
                'remote_ip' => $order->remote_ip,
            ];
            $model = new VoteRecordModel();
            $model->setAttributes($attributes);
            if ($model->save()) {
                $order->updateAttributes(['state' => OrderPayState::PAY]);
            } else {
                Yii::info($model->errors);
                return false;
            }
        });
        return $response;
    }

}
